#使用邻接列表表示图
g={'A':['B','C'],
   'B':['A','F'],
   'C':['A','D','F','E'],
   'D':['C'],
   'E':['C','F'],
   'F':['B','C','E']}
print('所有的节点是:')
for k in g:
    print(k,end='')
print()
print('所有的边如下：')
for k in g:
    n=g[k]
    for i in n:
        print('{}->{}'.format(k,i))
        

        
#邻接矩阵的方式表示图 
#vs表示点的集合
#es表示边的集合     
vs=['A','B','C']
es=[[0,1,1],[0,0,1],[0,0,0]]
for i in range (len(es)):
    for j in range (len(es[i])):
        if es[i][j]==1:
            print(vs[i],"->",vs[j])
            
            
            
#栈的方式实现图的优先遍历
def dfs_stack(g,start_vertex):
    s=[start_vertex]
    visited=[]
    while len(s)>0:
        v=s.pop(-1)
        if v in visited:
            continue
        else:
            visited.append(v)
            s.extend(g[v])
    return visited
g={'A':['B','C'],
   'B':['A','F'],
   'C':['A','D','F','E'],
   'D':['C'],
   'E':['C','F'],
   'F':['B','C','E']}
def test():
    print("深度优先遍历（栈实现）", dfs_stack(g,'A'))
if __name__=='__main__':
   test()            

  
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
            
        
    